package demo01.web;

import demo01.entity.JsonResponse;
import demo01.entity.User;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zhaott on 2018/9/18.
 */

@org.springframework.web.bind.annotation.RestController
@RequestMapping("/rest")
public class RestController {


    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResponse details(@PathVariable("id") String id){
        return JsonResponse.success(new User(id));
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public JsonResponse delete(@PathVariable("id") String id){
        return JsonResponse.message("删除成功！");
    }

    /**
     * 保存
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public JsonResponse save(@RequestBody User user){
        if(user==null){
            return JsonResponse.error("未获取到用户信息");
        }else{
            return JsonResponse.success(user);
        }
    }

    /**
     * 更新
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public JsonResponse update(@RequestBody User user){
        if(user==null||user.getId() == null){
            return JsonResponse.error(user==null?"未获取到用户信息":"用户ID不能为空");
        }else{
            return JsonResponse.success(user);
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public JsonResponse paging(@RequestParam(value="pageSize",defaultValue = "20") int pageSize,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum){
        return JsonResponse.success("pageSize:"+pageSize+" , pageNum:"+pageNum);
    }

}
